package com.jinzhi.jzapi.certificate.filter;

import com.alibaba.fastjson.JSONObject;
import com.jinzhi.common.configconsts.wxconsts.LinkConfiggure;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.jzweb.domain.CapitalFlowDO;
import com.jinzhi.jzweb.domain.OrderDO;
import com.jinzhi.jzweb.domain.StuSignUpDO;
import com.jinzhi.jzweb.service.CapitalFlowService;
import com.jinzhi.jzweb.service.OrderService;
import com.jinzhi.jzweb.service.StuSignUpService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 微信回调之 变更资金流水信息
 */
@Slf4j
@Component
public class WxNotifySignUpFilter implements Command {

    @Autowired
    private CapitalFlowService capitalFlowService;
    @Autowired
    private StuSignUpService stuSignUpService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @Override
    public boolean execute(Context context) throws Exception {

        JSONObject mapToJson = (JSONObject) context.get(LinkConfiggure.WX_JSON);

        try {
            log.info("正式开始下单回调业务处理");
            String return_code = mapToJson.getString("return_code");
            if (LinkConfiggure.SUCCESS_CODE.equals(return_code)) {
                String out_trade_no = mapToJson.getString("out_trade_no");
                Long stuSignUpId = Long.parseLong(EhCacheUtil.get(out_trade_no + ":stuSignUpId").toString());
                log.info("stuSignUpId:" + stuSignUpId);
                if(stuSignUpId != null){
                    //查询资金流水
                    CapitalFlowDO capitalFlowDO = capitalFlowService.findOneByKv("order_id",stuSignUpId);
                    if(capitalFlowDO != null){
                        if(!capitalFlowDO.getState().equals(0)){
                            //更新资金流水信息
                            capitalFlowDO.setState(0);//有效状态（0成功，1失败，2待确认，9已删除）
                            UserDO userDO = userService.selectById(capitalFlowDO.getUserId());
                            if(userDO != null){
                                capitalFlowDO.setBalances(userDO.getMoney());//用户余额
                            }
                            capitalFlowService.updateById(capitalFlowDO);
                        }

                        //更新考生报名信息状态
                        StuSignUpDO stuSignUpDO = stuSignUpService.selectById(stuSignUpId);
                        if(stuSignUpDO != null){
                            if(stuSignUpDO.getState().equals(1)){
                                stuSignUpDO.setState(4);//审核状态（0待审核，1待缴费，2未通过，3已分配，4已缴费，9已删除）
                                stuSignUpService.updateById(stuSignUpDO);
                            }
                        }

                        //更新订单信息状态
                        OrderDO orderDO = orderService.findOneByKv("object_id",stuSignUpId);
                        if(orderDO != null){
                            if(orderDO.getState().equals(1)){
                                orderDO.setState(0);//审核状态（0已付款，1未付款，2已退款，3已到期，9已删除）
                                orderService.updateById(orderDO);
                            }
                        }
                    }
                }
                context.put(LinkConfiggure.WX_ORDER_INFO_DO, "");//返回信息
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
